package com.chenglu.android.supermarket;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.chenglu.android.ComnonPagerAdapter;
import com.chenglu.android.R;
import com.chenglu.android.TabEntity;
import com.chenglu.android.cart.CartFragment;
import com.chenglu.android.common.base.BaseFullScreenActivity;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.Utils;
import com.chenglu.android.common.widget.NotScrollViewPager;
import com.chenglu.android.index.IndexFragment;
import com.chenglu.android.message.MessageFragment;
import com.chenglu.android.mine.login.LoginActivity;
import com.chenglu.android.mine.me.MineFragment;
import com.chenglu.android.onelogin.OneKeyLoginActivity;
import com.chenglu.android.redpacket.RedPacketFragement;
import com.chenglu.android.supermarket.fragment.MarketHomeFragment;
import com.chenglu.android.supermarket.fragment.MarketOrderFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: lyj
 * @Created: by Copyright 2023/9/22 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：商超
 */
public class SuperMarkerActivity extends BaseFullScreenActivity {

    @BindView(R.id.main_view_pager)
    NotScrollViewPager mainViewPager;
    @BindView(R.id.main_tablayout)
    CommonTabLayout mainTablayout;

    private int tabPosition = 0;

    private String mTitles[] = new String[]{"首页", "订单"};

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    ComnonPagerAdapter mComnonPagerAdapter;

    public final static String pid = "pid";

    public String getPid;

    //自定义的Icon
    private int[] mTabIcon = new int[]{
            R.drawable.tab_icon_markethome_default,
            R.drawable.tab_icon_marketorder_default
    };
    //自定义选择器 tab_bar_fudou_h
    private int[] mTabIconSelected = new int[]{
            R.drawable.tab_icon_markethome_press,
            R.drawable.tab_icon_marketorder_press
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_super_market);
        ButterKnife.bind(this);
        getPid = getIntent().getStringExtra(pid);
        initFragment();

    }


    private void initFragment() {

        List<Fragment> list = new ArrayList<>();
        list.add(MarketHomeFragment.newInstance(this));
        list.add(MarketOrderFragment.newInstance(this));

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mTabIconSelected[i], mTabIcon[i]));
        }
        mainTablayout.setTabData(mTabEntities);
        mComnonPagerAdapter = new ComnonPagerAdapter(getSupportFragmentManager(), list);
        mainViewPager.setAdapter(mComnonPagerAdapter);
        mainViewPager.setOffscreenPageLimit(list.size());
        mainTablayout.setCurrentTab(0);
//        mainTablayout.setCurrentTab(0);
        mainViewPager.setCurrentItem(0, false);
        mainTablayout.setOnTabSelectListener(new OnTabSelectListener() {

            @Override
            public void onTabSelect(int position) {
                LogUtils.e("mainTablayout- position:" + position);
                if (position == 1) {
                    if (!isLogin) {
                        mainViewPager.setCurrentItem(0, false);
                        mainTablayout.setCurrentTab(0);
                        if (Utils.checkSim(mContext)) {
                            //判断是否是模拟器
                            if (Utils.isEmulator()) {
                                startActivity(new Intent(mContext, LoginActivity.class));
                                return;
                            } else {
                                startActivity(new Intent(mContext, OneKeyLoginActivity.class));
                                return;
                            }

                        } else {
                            startActivity(new Intent(mContext, LoginActivity.class));
                            return;
                        }
                    }

                }
                switch (position) {
                    case 0:

                        tabPosition = position;
                        mainViewPager.setCurrentItem(position, false);
                        mainTablayout.setCurrentTab(position);
                        break;
                    case 1:
                        tabPosition = position;
                        mainViewPager.setCurrentItem(position, false);
                        mainTablayout.setCurrentTab(position);
                        break;
                }

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }


}
